package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.nntp.Article;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author shiminghui
 * @date 2024/10/30 17:31
 * @description: TODO
 */
@Service
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    private final static int MAX_PAGE_SIZE = 50;

    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;

    @Override
    public ResponseResult loadArticleList(ArticleHomeDto dto, Short type) {
        //校验参数
        if (StringUtils.isEmpty(dto.getTag())) {
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        if (dto.getSize() == null || dto.getSize() == 0) {
            dto.setSize(10);
        }

        if (dto.getSize() > MAX_PAGE_SIZE) {
            dto.setSize(MAX_PAGE_SIZE);
        }

        if (dto.getMaxBehotTime() == null) {
            dto.setMaxBehotTime(new Date());
        }
        if (dto.getMinBehotTime() == null) {
            dto.setMinBehotTime(new Date());
        }

        if (type == null || !type.equals(ArticleConstants.LOADTYPE_LOAD_MORE) || !type.equals(ArticleConstants.LOADTYPE_LOAD_NEW)) {
            type = ArticleConstants.LOADTYPE_LOAD_MORE;
        }

        //查询数据
        List<ApArticle> apArticles = apArticleMapper.loadArticleList(dto, type);

        return ResponseResult.okResult(apArticles);
    }

    /**
     * 保存App文章
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public ResponseResult saveArticle(ArticleDto dto) {
        log.info("保存文章：{}", dto);
        //1.检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (dto.getContent() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断是否存在
        ApArticle article = new ApArticle();
        if (dto.getId() == null) {
            //2.1 不存在 添加 文章 文章内容 文章配置
            BeanUtils.copyProperties(dto, article);
            save(article);

            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(article.getId());
            apArticleContent.setContent(dto.getContent());
            int insert = apArticleContentMapper.insert(apArticleContent);
            if (insert != 1) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "文章内容保存失败");
            }

            ApArticleConfig apArticleConfig = new ApArticleConfig(article.getId());
            int insert_config = apArticleConfigMapper.insert(apArticleConfig);
            if (insert_config != 1) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "文章配置保存失败");
            }

            dto.setId(article.getId());

        } else {
            //2.2.1 存在 更新 文章 文章内容
            article = apArticleMapper.selectOne(Wrappers.<ApArticle>lambdaQuery().eq(ApArticle::getId, dto.getId()));
            if (article == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "文章不存在");
            }
            BeanUtils.copyProperties(dto, article);
            apArticleMapper.updateById(article);

            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getId()));
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.updateById(apArticleContent);

        }

        //3.生成静态页面
        articleFreemarkerService.buildArticleToMinIO(article, dto.getContent());


        return ResponseResult.okResult(dto.getId());
    }
}
